<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" title="设置角色权限" width="700px">
    <el-form ref="form" :model="form" size="small" label-width="80px">
      <el-form-item label="上级角色">
        <treeselect v-model="form.super_role_id" :options="roles" style="width: 370px;" placeholder="请选择角色" />
      </el-form-item>
      <el-form-item label="成员角色">
        <treeselect v-model="form.ordinary_role_id" :options="roles" style="width: 370px;" placeholder="请选择角色" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSveRole">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  components: { Treeselect },
  props: {
    roles: {
      type: Array,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false,
      dialog: false,
      form: {
        id: '',
        super_role_id: '',
        ordinary_role_id: ''
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    doSveRole() {
      this.$api.enterprise.department
        .saveRole({
          data: this.form
        })
        .then(res => {
          this.resetForm()
          this.$notify({
            title: '修改成功',
            type: 'success',
            duration: 2500
          })
          this.loading = false
          this.sup_this.init()
        }).catch(err => {
          this.loading = false
          console.log(err.response.data.message)
        })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        super_role_id: '',
        ordinary_role_id: ''
      }
    }
  }
}
</script>

<style scoped>

</style>
